package com.ysy.factory;

import com.ysy.enums.StorageType;
import com.ysy.strategy.FileUploadStrategy;
import com.ysy.strategy.impl.MinioUploadStrategy;
import jakarta.annotation.PostConstruct;


import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.stereotype.Component;


import java.util.HashMap;
import java.util.Map;

@Component
@RefreshScope
public class FileUploadStrategyFactory {

    private final  Map<Integer,FileUploadStrategy> map = new HashMap();

    @Value("${file.upload.type}")
    private String fileUploadType;

//    static {
//        MARK_MAP.put(StorageType.MINIO.getType(), new MinioUploadStrategy());
//    }
    @Resource
    private MinioUploadStrategy minioUploadStrategy;

    @PostConstruct
   // @EventListener(ContextRefreshedEvent.class)
    public void init() {
        map.clear();
        if (fileUploadType.contains(StorageType.MINIO.getType().toString())) {
            map.put(StorageType.MINIO.getType(), minioUploadStrategy);
        }
    }

    public  FileUploadStrategy getFileUploadStrategy(Integer type){
        if (null==map.get(type)) {
            throw new  RuntimeException("找不到策略类~");
        }
        return  map.get(type);
    }


}

